package org.jks.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jks.model.Province;
import org.jks.response.model.ActionResult;
import org.jks.response.model.UITable;
import org.jks.service.ProvinceService;
import org.jks.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/province")
public class ProvinceController {
	@Autowired
	ProvinceService provinceService;

	@RequestMapping("/all")
	public void all(HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			List<Province> provinces = provinceService.all();
			if(provinces != null)
			{
				for(Province prov : provinces){
					prov.setCities(null);
				}
				ar.setSuccess(true);
				UITable table = new UITable();
				table.setRows(provinces);
				ar.setData(table);
			}
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/getAllCities/{id}")
	public void getAllCityById(@PathVariable long id, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			Province province = provinceService.findById(id);
			if(province != null)
			{
				province.getCities().size();
				ar.setSuccess(true);
				UITable table = new UITable();
				table.setRows(province);
				ar.setData(table);
			}
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
}
